﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Criterion;
using microCRM.Domain;

namespace microCRM
{
    class DBHelper
    {
        private static ISessionFactory sessions;

        public static void Configure()
        {
            sessions = new Configuration().Configure().
                            AddClass(typeof(Account)).
                            AddClass(typeof(Client)).
                            AddClass(typeof(Contract)).
                            AddClass(typeof(FinansialOperation)).
                            AddClass(typeof(Number)).
                            AddClass(typeof(NumberType)).
                            AddClass(typeof(Office)).
                            AddClass(typeof(OperationType)).
                            AddClass(typeof(Operator)).
                            AddClass(typeof(Plan)).
                            AddClass(typeof(Service)).
                            AddClass(typeof(SimCard)).
                            AddClass(typeof(Transaction)).
                            AddClass(typeof(TransactionType)).
                            BuildSessionFactory();
        }

        public static void CreateTables()
        {
            Configuration configuration = new Configuration();
            configuration.Configure();
            configuration.AddClass(typeof(Account));
            configuration.AddClass(typeof(Client));
            configuration.AddClass(typeof(Contract));
            configuration.AddClass(typeof(FinansialOperation));
            configuration.AddClass(typeof(Number));
            configuration.AddClass(typeof(NumberType));
            configuration.AddClass(typeof(Office));
            configuration.AddClass(typeof(OperationType));
            configuration.AddClass(typeof(Operator));
            configuration.AddClass(typeof(Plan));
            configuration.AddClass(typeof(Service));
            configuration.AddClass(typeof(SimCard));
            configuration.AddClass(typeof(Transaction));
            configuration.AddClass(typeof(TransactionType));
            SchemaExport schemaExport = new SchemaExport(configuration);
            schemaExport.Execute(false, true, false);
        }

        #region Client
        public static void CreateClient(Client client)
        {
            using (ISession session = sessions.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(client);
                tx.Commit();
            }
        }

        public static void UpdateClient(Client client)
        {
            using (ISession session = sessions.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(client);
                tx.Commit();
            }
        }
        public static void DeleteClient(Client client)
        {
            using (ISession session = sessions.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Delete(client);
                tx.Commit();
            }
        }

        public static IList<Client> FindAbonent(string firstName, string middleName, string lastName)
        {
            using (ISession sess = sessions.OpenSession())
            {
                return sess
                    .CreateCriteria(typeof(Client))
                    .Add(Expression.Like("FirstName", "%" + firstName + "%"))
                    .Add(Expression.Like("MiddleName", "%" + middleName + "%"))
                    .Add(Expression.Like("LastName", "%" + lastName + "%"))
                    .List<Client>();
            }
        }

        public static Client GetAbonentByID(int id)
        {
            using (ISession sess = sessions.OpenSession())
            {
                return sess
                    .CreateCriteria(typeof(Client))
                    .Add(Expression.Like("ID", id))
                    .List<Client>().ElementAt<Client>(0);
            }
        }
        #endregion
        #region Plan
        public static void CreatePlan(Plan plan)
        {
            using (ISession session = sessions.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(plan);
                tx.Commit();
            }
        }

        public static void UpdatePlan(Plan plan)
        {
            using (ISession session = sessions.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(plan);
                tx.Commit();
            }
        }
        public static void DeletePlan(Plan plan)
        {
            using (ISession session = sessions.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Delete(plan);
                tx.Commit();
            }
        }
        public static IList<Plan> GetPlans()
        {
            using (ISession sess = sessions.OpenSession())
            {
                return sess
                    .CreateCriteria(typeof(Plan))
                    .List<Plan>();
            }
        }
        #endregion
    }
}
